﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Cloud.Identity.QuickStarts.Controllers
{

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
    //        var client_id = System.Configuration.ConfigurationManager.AppSettings["ClientId"];
    //        var client_secret = System.Configuration.ConfigurationManager.AppSettings["ClientSecret"];
    //        var TokenServiceEndpoint = System.Configuration.ConfigurationManager.AppSettings["TokenEndpoint"];

    //        var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(client_id + ":" + client_secret));

    //        var httpClient = new HttpClient();
    //        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
    //        var httpContent = new FormUrlEncodedContent(new
    //        Dictionary<string, string>
    //{
    //    {"grant_type", "client_credentials"}
    //});

    //        var response = httpClient.PostAsync(TokenServiceEndpoint, httpContent).Result;

    //        var responseContent = response.Content.ReadAsStringAsync().Result;
    //        string accessToken =null;
    //        if (response.StatusCode == System.Net.HttpStatusCode.OK)
    //        {
    //            accessToken = JObject.Parse(responseContent)["access_token"].ToString();
    //        }
    //        else
    //        {

    //        }
    //        var UserDataServiceEndpoint = System.Configuration.ConfigurationManager.AppSettings["UserDataServiceEndpoint"];
    //        var builder = new UriBuilder(UserDataServiceEndpoint);
    //        httpClient = new HttpClient();
    //        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
    //            accessToken);
    //        var result = httpClient.GetAsync(builder.Uri);
    //        var userObject = result.Result.Content.ReadAsAsync<Dictionary<string, string>>().Result;

            return RedirectToAction("Index", "Me");
        }
    }
}